package com.seo.service.redisUtil.impl;

import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.constants.RedisConstant;
import com.seo.service.redis.JedisUtil;
import com.seo.service.redisUtil.RedisUtilService;
import com.seo.utils.CustomUtil;
import com.seo.utils.ShortUrlUtil;

/**
 * redis相关service
 * 
 * @author Administrator
 * @version 2018.08.02 17:40
 */
@Service("redisUtilService")
public class RedisUtilServiceImpl implements RedisUtilService {

    @Autowired
    private JedisUtil jedisService;

    @Override
    public String makeUserToken(int userId, String key) {
        return makeUserToken(Integer.toString(userId), key);
    }

    @Override
    public String makeUserToken(String userId, String key) {
        String nonce = ShortUrlUtil.buildCode(8);
        String timestamp = Long.toString(System.currentTimeMillis());
        Map<String, String> params = new TreeMap<String, String>();
        params.put("userId", userId);
        params.put("nonce", nonce);
        params.put("timestamp", timestamp);
        String userToken = CustomUtil.createSign(params, key);
        return userToken;
    }

    @Override
    public String getUserToken(String key) {
        return jedisService.get(key);
    }

    @Override
    public void setUserToken(String key, String value) {
        jedisService.set(key, value);
        jedisService.expire(key, 1800);
    }

    @Override
    public void setUserToken(String key, String value, int expire) {
        jedisService.set(key, value);
        jedisService.expire(key, expire);

    }

    @Override
    public void setUserTokenExpire(String key) {
        jedisService.expire(key, 1800);
    }
    
    @Override
    public void setUserToken(String userId,String loginId, String userToken) {
        String value = userId + "_" + loginId;
        setUserToken(RedisConstant.WAP_USER_TOOKEN + userToken, value);
    }
    @Override
    public void removeUserToken(String key) {
        jedisService.del(key);
    }

    @Override
    public boolean isExists(String key) throws Exception {
        return jedisService.exists(key);
    }
}
